package com.example.getcoordinatesdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * @author Li Xuyang
 * @date 2020/7/2 01:22
 */
/*只不过一个是继承的view一个是surfaceview，将AttributeSetattrs加上。只要处理好谁是顶层的view谁设成透明，预览视频的surfaceview设成底层，在且要在xml属性文件里设成visible就可以了*/
public class SVDraw extends SurfaceView implements SurfaceHolder.Callback{


    private Paint mPaint = null; //

    private Bitmap mBitmap ;
    private Canvas mCanvas;
    Path mPath;
    protected SurfaceHolder sh;
    private int mWidth;
    private int mHeight;
    private Thread thread;

    public SVDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        sh = this.getHolder();
        sh.addCallback(this);
        sh.setFormat(PixelFormat.TRANSPARENT);
        setZOrderOnTop(true);

        mPath=new Path();

        mPaint=new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(15);


        //获取屏幕的宽高
        Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
        int screenHeight = display.getHeight();
        int screenWidth = display.getWidth();

        //生成自己的bitmap和mCanves
        mBitmap = Bitmap.createBitmap(screenWidth,screenHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);


    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int w, int h) {
        mWidth = w;
        mHeight = h;
    }

    public void surfaceCreated(SurfaceHolder arg0) {

    }

    public void surfaceDestroyed(SurfaceHolder arg0) {

    }


   public void clearDraw()
    {
        invalidate();
        Canvas canvas = sh.lockCanvas();
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        sh.unlockCanvasAndPost(canvas);
    }
    public void drawLine()
    {
        thread = new Thread();
        thread.run();
        thread.start();
    }



    private void draw(){
        mCanvas=sh.lockCanvas();
        try {
            //绘制
           mCanvas.drawPath(mPath,mPaint);
            invalidate();
            Thread.sleep(10);

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if (mCanvas!=null) {
                sh.unlockCanvasAndPost(mCanvas);
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //新建路径
                mPath=new Path();
                //移动到起始位置
                mPath.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                //绘制路径
                mPath.lineTo(x, y);
                //绘制路径到views上
                draw();
                break;
            case MotionEvent.ACTION_UP:
                clearDraw();
                break;
            default:
                break;
        }
        System.out.println(x+","+y);

        return true;
    }



}
